package com.banyuan.servlet.opus;


import com.banyuan.dao.OpusDao;
import com.banyuan.dao.TypesDao;
import com.banyuan.domain.Opus;
import com.banyuan.domain.Types;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/opus/updatePage")
public class UpdatePageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 要把types表里所有的信息发送给前端，还有opus表里的所有信息
        String id = req.getParameter("id");
        ResultSet resultSet = null;
        ResultSet rs = null;



        try {
            // 把types表里所有的信息发送给前端
            resultSet = TypesDao.selectAll("", "");
            List<Types> typesList = DBUtils.resultToBeanAll(resultSet, Types.class);
            req.setAttribute("typeList",typesList);

            //opus表里的信息发送给前端

            rs = OpusDao.selectOpusById(Integer.parseInt(id));
            Opus opus = DBUtils.resultToBeanOne(rs, Opus.class);
            req.setAttribute("opus",opus);
            req.getRequestDispatcher("/WEB-INF/pages/opus/update.jsp").forward(req,resp);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
